<template>
  <!-- 收款管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/contractManagement']"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <!-- <el-input
            v-model="searchValue.search"
            placeholder="收款名称"
            clearable
          ></el-input> -->
          <sc-table-select
            v-model="customerData"
            :apiObj="customerApiObj"
            style="width: 100%"
            table-width="100%"
            clearable
            collapse-tags
            collapse-tags-tooltip
            @change="customerChange"
            :props="customerProps"
          >
            <template #header="{ form, submit }">
              <el-form :inline="true" :model="form">
                <el-form-item>
                  <el-input
                    v-model="form.search"
                    placeholder="客户名称"
                    clearable
                  ></el-input>
                </el-form-item>
                <el-button type="primary" @click="submit">查询</el-button>
              </el-form>
            </template>
            <el-table-column
              label="客户名称"
              prop="customerName"
              min-width="150"
            ></el-table-column>
            <el-table-column
              label="客户类型"
              prop="customertypeName"
              width="150"
            >
              <template #default="scope">
                <p v-if="scope.row.customertype == 1">普通客户</p>
                <p v-if="scope.row.customertype == 2">重要客户</p>
              </template>
            </el-table-column>
            <el-table-column label="联系人" prop="firstcontacter" width="150">
            </el-table-column>
            <el-table-column
              label="联系人号码"
              prop="phone"
              width="200"
            ></el-table-column>
          </sc-table-select>
          <el-select
            v-model="searchValue.paymentStatus"
            placeholder="请选择收款状态"
            style="width: 100%"
            clearable
          >
            <el-option label="未收款" :value="0" />
            <el-option label="已收款" :value="1" />
          </el-select>
          <el-select
            v-model="searchValue.type"
            placeholder="请选择收款类型"
            style="width: 100%"
            clearable
          >
            <el-option label="客户" :value="3" />
            <el-option label="其他" :value="2" />
          </el-select>
          <el-date-picker
            v-model="searchValue.startTime"
            type="date"
            placeholder="选择开始日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          >
          </el-date-picker>
          <el-date-picker
            v-model="searchValue.endTime"
            type="date"
            placeholder="选择结束日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          >
          </el-date-picker>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <div class="count" v-auths="['financeCount']">
      <p>
        收款总额：{{
          countMoney.totalPaymentMoney == null
            ? 0
            : countMoney.totalPaymentMoney
        }}元
      </p>
      <p>
        收款年总额：{{
          countMoney.yearPaymentMoney == null ? 0 : countMoney.yearPaymentMoney
        }}元
      </p>
      <p>
        收款月总额：{{
          countMoney.monthPaymentMoney == null
            ? 0
            : countMoney.monthPaymentMoney
        }}元
      </p>
    </div>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @filterType="filterType"
        @row-dblclick="handleRowDblclick"
        :showSummary="true"
        :summary-method="getSummaries"
      >
        <el-table-column label="ID" prop="id" width="50"></el-table-column>
        <el-table-column
          label="收款名称"
          prop="paymentName"
          min-width="250"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="类型" width="100">
          <template #default="scope">
            <p v-if="scope.row.paymentType == 1">合同收款</p>
            <p v-if="scope.row.paymentType == 2">其他收款</p>
            <p v-if="scope.row.paymentType == 3">客户收款</p>
          </template>
        </el-table-column>
        <el-table-column label="收款总金额" prop="money" width="100">
          <template #default="scope">
            <p>{{ scope.row.money }}元</p>
          </template>
        </el-table-column>
        <el-table-column label="收款方式" prop="repaymentTypeName" width="100">
        </el-table-column>
        <el-table-column label="收款日期" prop="createTime" width="150">
        </el-table-column>
        <el-table-column label="添加人" prop="adminName" width="100">
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          :show-overflow-tooltip="true"
          width="150"
        >
        </el-table-column>
        <el-table-column label="收款状态" prop="paymentStatus" width="100">
          <template #default="scope">
            <p v-if="scope.row.paymentStatus == 1" style="color: #67c23a">
              已收款
            </p>
            <p v-if="scope.row.paymentStatus == 0" style="color: #f56c6c">
              未收款
            </p>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="审核状态"
          prop="status"
          width="100"
          fixed="right"
        >
          <template #default="scope">
            <p v-if="scope.row.status == 0">未审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3">作废</p>
            <p v-if="scope.row.status == 4">删除</p>
          </template>
        </el-table-column> -->

        <el-table-column label="操作" fixed="right" align="center" width="300">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_confirm(scope.row, scope.$index)"
                v-if="scope.row.paymentStatus == 0"
                v-auth-button="['confirm/contractManagement']"
                >确认收款</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                v-auth-button="['show/contractManagement']"
                >查看</el-button
              >
              <!-- <el-button
                text
                type="primary"
                size="small"
                @click="table_showAll(scope.row, scope.$index)"
                v-if="scope.row.paymentType == 1"
                >查看合同</el-button
              > -->
              <!-- <el-button
                v-if="scope.row.status == 0"
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-auth-button="['edit/contractManagement']"
                >编辑</el-button
              >
              <el-popconfirm
                v-if="scope.row.status == 0"
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['delete/contractManagement']"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm> -->
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>

<script>
import { queryTypeByDic } from "@/utils/getData"
export default {
  name: "collectManagement",

  data () {
    return {
      apiObj: this.$API.finance.collectManagement.getCollectList,
      customerApiObj: this.$API.customer.getCustomers,
      customerData: {},
      customerProps: {
        label: 'customerName',
        value: 'id',
      },
      searchValue: {
        search: null,
        status: "",
        paymentStatus: null,
        startTime: "",
        endTime: "",
        type: ''
      },
      columnsFilters: ["money"],
      countMoney: [],
    }
  },
  mounted () {
    //this.getCustomerList()
    this.getCountMoney()
  },
  methods: {
    //列表统计
    async getCountMoney () {
      var res =
        await this.$API.finance.collectManagement.getCountMoney.get()
      if (res.data != null) {
        this.countMoney = res.data
      }
    },
    //添加
    add () {
      // this.dialog.save = true
      // this.$nextTick(() => {
      //   this.$refs.saveDialog.open()
      // })
      this.$router.push({
        path: "/finance/collectManagement/collectAdd",
        query: {
          mode: "add",
        },
      })
    },
    //合计行统计
    getSummaries (param) {
      console.log(param)
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "页合计"
          return
        }
        if (this.columnsFilters.includes(column.property)) {
          const values = data.map((item) =>
            Number(item[column.property])
          )
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] += " 元"
          } else {
            sums[index] = ""
          }
        }
      })
      return sums
    },
    //编辑
    table_edit (row) {
      let str = row.status == 3 ? "审核中" : "已通过审核"
      if (row.status !== 0) {
        this.$message.error(`收款${str}，不能编辑`)
        return
      }
      this.$router.push({
        path: "/finance/collectManagement/collectAdd",
        query: {
          id: row.id,
          mode: "edit",
        },
      })
    },
    // 确认收款
    table_confirm (row) {
      this.$confirm("请确认是否已经完成收款?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res =
            await this.$API.finance.collectManagement.updateCollect.post(
              { id: row.id, paymentStatus: 1 }
            )
          if (res.code == 200) {
            this.$refs.table.refresh()
            this.$message.success("操作成功")
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消确认收款",
          })
        })
    },
    //查看
    table_show (row) {
      this.$router.push({
        path: "/finance/collectManagement/collectDetail",
        query: {
          id: row.id,
        },
      })
    },
    // 查看合同总详情
    table_showAll (row) {
      console.error(row)
      this.$router.push({
        path: "/contract/contractManagement/detailList",
        query: {
          id: row.contractId,
        },
      })
    },
    //删除
    async table_del (row) {
      console.log(row)
      let str = row.status == 3 ? "审核中" : "已通过审核"
      if (row.status !== 0) {
        this.$message.error(`收款${str}，不能删除`)
        return
      }
      let data = {
        id: row.id,
        projectId: row.projectId,
      }
      var res =
        await this.$API.finance.collectManagement.deleteCollect.post(
          data
        )
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("删除成功")
      }
    },

    // 客户数据
    customerChange (val) {
      console.log(val)
      this.searchValue.customerId = val.id

    },

    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    //本地更新数据
    handleSaveSuccess (data, mode) {
      if (mode == "add") {
        // this.$refs.table.refresh()
      } else if (mode == "edit") {
        // this.$refs.table.refresh()
      }
      this.$refs.table.refresh()
    },
    // 处理收款方式
    filterType (data) {
      let tableData = data
      queryTypeByDic("payment").then((res) => {
        let list = res
        tableData.forEach((item) => {
          list.forEach((f_item) => {
            if (item.repaymentType == f_item.value) {
              item.repaymentTypeName = f_item.name
            }
          })
        })
      })
    },
    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style lang="scss" scoped>
.count {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 50px;
  padding-left: 12px;
  font-size: 14px;
  color: #555;
  font-weight: 600;
  p {
    margin-right: 20px;
  }
}
</style>
